//
//  SLMessageCenterViewModel.m
//  StarLight
//
//  Created by 吴涛 on 16/6/6.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLMessageCenterViewModel.h"
#import "SLNetWork.h"


@interface SLMessageCenterViewModel ()

@property (nonatomic, strong) NSString *requstPath;

@end


@implementation SLMessageCenterViewModel


- (instancetype)init{
  if (self = [super init]) {
    
    NSString *uid = [DEFAULTS objectForKey:loginInfo][@"id"];
    NSString *url = [NSString stringWithFormat:@"%@/sys/meslist?uid=%@",baseUrl,uid];
    
    self.requstPath = url;
    [self addObserver];
  }
  return self;
}


- (void)addObserver{
  @weakify(self)

  [[RACObserve(self, requstPath) filter:^BOOL(id value) {
    return value != nil;
  }] subscribeNext:^(NSString *path) {

    [SLNetWork getDataWithRequest:path complete:^(id respond, NSError *error) {
      @strongify(self)
      
      if (!error) {
        NSMutableArray *arr = [SLMessageModel mj_objectArrayWithKeyValuesArray:respond[@"data"]];
        
        self.msgList = arr;
        
      }
      
    }];
    
  }];
  
}

- (void)first{
  NSString *uid = [DEFAULTS objectForKey:loginInfo][@"id"];
  NSString *url = [NSString stringWithFormat:@"%@/sys/meslist?uid=%@",baseUrl,uid];
  
  self.requstPath = url;
}


- (void)deleteMsgWithMid:(NSString *)mid{
  @weakify(self)
  NSString *uid = [DEFAULTS objectForKey:loginInfo][@"id"];
  NSDictionary *para = @{@"uid":uid,@"mid":mid};
  NSString *postUrl = [NSString backUrlWithService:@"sys" methodName:@"delmes"];
  [SLNetWork postDataWithParameters:para postUrl:postUrl complete:^(id respond, NSError *error) {
    @strongify(self)
    [self first];
  }];
}


@end



@implementation SLMessageModel

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
  return @{@"mid" : @"id"};
}

@end

